package com.online.edu.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.online.edu.course.entity.Category;
import com.online.edu.course.entity.Vo.CategoryVo;
import com.online.edu.course.entity.Vo.QueryCategoryVo;
import com.online.edu.course.service.ICategoryService;
import com.online.edu.util.core.Result;
import com.online.edu.util.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程分类表 前端控制器
 *
 * @author world
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/course/category")
@Api(tags = "课程分类表接口")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;

    //获取课程分类表
    @ApiOperation("获取课程分类表")
    @GetMapping("/getCategory")
    public Result getCategory(){
        List<CategoryVo> categoryVoList = categoryService.listWithTree();
        return Result.ok(categoryVoList);
    }

    //多条件查询列表
    @ApiOperation("多条件查询列表")
    @PostMapping("/query")
    public Result<List<Category>> query(@RequestBody QueryCategoryVo queryCategoryVo){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if (queryCategoryVo.getCategoryName() != null){
            queryWrapper.like("category_name",queryCategoryVo.getCategoryName());
        }
        if (queryCategoryVo.getStatusId() != null){
            queryWrapper.eq("status_id",queryCategoryVo.getStatusId());
        }
        List<Category> categoryList = categoryService.list(queryWrapper);
        return Result.ok(categoryList);
    }

    //判断更新or新增
    @ApiOperation("判断更新or新增")
    @PostMapping("/save")
    public Result save(Category category){
        if (category.getId() != null){//id不为空则为更新操作
            boolean b = categoryService.updateById(category);
            if (b){
                return Result.success("更新成功！");
            }
            else {
                return Result.error("更新失败！");
            }
        }
        else {
            category.setId(Long.valueOf(StringUtil.uuid()));//设置列表id
            category.setParentId(0L);//设置父id为0
            boolean b = categoryService.save(category);
            if (b){
                return Result.success("插入成功！");
            }
            else {
                return Result.error("插入失败！");
            }
        }
    }

    //新增子列表
    @ApiOperation("新增子列表")
    @PostMapping("/insertChild/{id}")
    public Result insertChild(@RequestBody Category category,
                         @PathVariable("id") long id){
        //设置子列表id
        category.setId(Long.parseLong(StringUtil.uuid()));
        //设置子列表父id
        category.setParentId(id);
        boolean b = categoryService.save(category);
        if (b){
            return Result.success("子列表新增成功");
        }
        else {
            return Result.error("子列表新增失败");
        }
    }

    //状态
    @ApiOperation("状态控制")
    @PostMapping("/status/{id}")
    public Result status(@PathVariable("id") long id){
        Category category = categoryService.getById(id);
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        if (category.getStatusId() == 1){
            updateWrapper.set("status_id",0);
        }
        if (category.getStatusId() == 0){
            updateWrapper.set("status_id",1);
        }
        boolean b = categoryService.update(category,updateWrapper);
        if (b) {
            return Result.success("更新成功！");
        }
        else {
            return Result.error("更新失败！");
        }
    }

    //删除分类
    @ApiOperation("通过ID删除列表")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") long id) throws Exception {
        boolean b = categoryService.delCategory(id);
        if (b) {
            return Result.success("删除成功！");
        }
        else {
            return Result.error("删除失败！");
        }
    }

}
